package com.muje.android.toiletcard;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

//import com.larvalabs.svgandroid.SVG;
//import com.larvalabs.svgandroid.SVGParser;

public class DisplayActivity extends Activity {

	static Poker poker;

	private TextView message;
	private ImageView card;
	private TextView closeCounterView;
	//private TextView openCounterView;//not necessary
	private ImageButton previousButton;
	private ImageButton nextButton;

	private Button playerButton1;
	private Button playerButton2;
	private Button playerButton3;
	private Button playerButton4;
	private Button playerButton5;
	private Button playerButton6;
	private Button playerButton7;
	private Button playerButton8;
	private Button playerButton9;
	private Button playerButton10;
	private Button playerButton11;
	private Button playerButton12;
	private Button[] buttons;
	//private PlayerControl playerControl1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//openCounterView = (TextView) findViewById(R.id.openCounterView);
		closeCounterView = (TextView) findViewById(R.id.closeCounterView);

		// this.playerControl1 = (PlayerControl) findViewById(R.id.playerControl1);
		this.playerButton1 = (Button) findViewById(R.id.playerButton1);
		this.playerButton2 = (Button) findViewById(R.id.playerButton2);
		this.playerButton3 = (Button) findViewById(R.id.playerButton3);
		this.playerButton4 = (Button) findViewById(R.id.playerButton4);
		this.playerButton5 = (Button) findViewById(R.id.playerButton5);
		this.playerButton6 = (Button) findViewById(R.id.playerButton6);
		this.playerButton7 = (Button) findViewById(R.id.playerButton7);
		this.playerButton8 = (Button) findViewById(R.id.playerButton8);
		this.playerButton9 = (Button) findViewById(R.id.playerButton9);
		this.playerButton10 = (Button) findViewById(R.id.playerButton10);
		this.playerButton11 = (Button) findViewById(R.id.playerButton11);
		this.playerButton12 = (Button) findViewById(R.id.playerButton12);
		this.buttons = new Button[] { playerButton1, playerButton2,
				playerButton3, playerButton4, playerButton5, playerButton6,
				playerButton7, playerButton8, playerButton9, playerButton10,
				playerButton11, playerButton12 };

		previousButton = (ImageButton) this.findViewById(R.id.previousButton);
		previousButton.setOnClickListener(previousOnClick);

		nextButton = (ImageButton) this.findViewById(R.id.nextButton);
		nextButton.setOnClickListener(nextOnClick);

		// TODO: svg support too buggy at this moment
		// @see http://code.google.com/p/svg-android/
		// SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.ad);
		// card.setImageDrawable(svg.createPictureDrawable());
		message = (TextView) this.findViewById(R.id.textView1);
		card = (ImageView) this.findViewById(R.id.cardView);
		card.setOnClickListener(nextOnClick);

		if (poker == null) {
			poker = new Poker(this);// maybe initial during menu or setup page
		}
		poker.shuffle();
		initialLayout();

		// first card not to show until user click on next button
		// show(null);
	}

	protected OnClickListener nextOnClick = new OnClickListener() {

		public void onClick(View v) {
			poker.next();
			show(v);
		}

	};
	protected OnClickListener previousOnClick = new OnClickListener() {

		public void onClick(View v) {
			poker.previous();
			show(v);
		}

	};

	/**
	 * Initialize the layout like player icon and some other control buttons.
	 */
	private void initialLayout() {

		//playerControl1.setPlayer(poker.Players.get(0));
		boolean hasPlayer = false;
		for (int i = 0; i < buttons.length; i++) {
			if (i >= poker.Players.size())
				buttons[i].setVisibility(View.INVISIBLE);
			else if (poker.Players.get(i) == null)
				buttons[i].setVisibility(View.INVISIBLE);// invisible
			else {
				hasPlayer = true;
				buttons[i].setText(poker.Players.get(i).Name);
			}
		}

		// hide the previous and next button
		if (!hasPlayer) {
			LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
			linearLayout2.setVisibility(View.INVISIBLE);// no previous button

			// direct start if no player
			poker.next();
			show(null);
		}
	}

	private void show(View v) {

		if (poker.Current.IsEnd == true) {
			
			//openCounterView.setText(Integer.toString(poker.CARD_COUNT));
			closeCounterView.setText(Integer.toString(0));
			
			AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
			alert.setMessage(getResources().getString(R.string.finish));// "Finish!"
			alert.show();
			
		} else {

			if (poker.Current != null) {
				// String msg = poker.Current.Id + ": " + poker.Current.Text +
				// "\n"
				String msg = poker.Current.RuleMessage;
				message.setText(msg);
				String fileName = "default" + File.separator + poker.Current.Id
						+ ".png";
				Log.d("DEBUG", "displaying " + fileName);
				try {
					card.setImageBitmap(getImageFromAsset(fileName));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			//openCounterView.setText(Integer.toString(poker.CARD_COUNT - poker.Cards.size() + 1));
			closeCounterView.setText(Integer.toString(poker.Cards.size() - 1));
		}
	}

	/**
	 * Get image from asset folder.
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	private Bitmap getImageFromAsset(String fileName) throws IOException {
		InputStream stream = null;
		try {
			stream = getAssets().open(fileName);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
		return BitmapFactory.decodeStream(stream);
	}

}